<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>售后管理系统 - 客服首页</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Microsoft YaHei', Arial, sans-serif;
            background-color: #f5f5f5;
        }

        .header {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 20px;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .header h1 {
            font-size: 24px;
        }

        .user-info {
            display: flex;
            align-items: center;
            gap: 20px;
        }

        .logout-btn {
            background: rgba(255, 255, 255, 0.2);
            border: 1px solid rgba(255, 255, 255, 0.3);
            color: white;
            padding: 8px 16px;
            border-radius: 5px;
            cursor: pointer;
            text-decoration: none;
        }

        .container {
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
        }

        .nav-menu {
            background: white;
            border-radius: 10px;
            padding: 20px;
            margin-bottom: 20px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
        }

        .nav-menu h2 {
            margin-bottom: 20px;
            color: #333;
        }

        .nav-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
            gap: 20px;
        }

        .nav-item {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 30px 20px;
            border-radius: 10px;
            text-align: center;
            cursor: pointer;
            transition: transform 0.2s ease;
            text-decoration: none;
            display: block;
        }

        .nav-item:hover {
            transform: translateY(-5px);
        }

        .nav-item h3 {
            font-size: 18px;
            margin-bottom: 10px;
        }

        .nav-item p {
            font-size: 14px;
            opacity: 0.9;
        }

        .stats-section {
            background: white;
            border-radius: 10px;
            padding: 20px;
            margin-bottom: 20px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
        }

        .stats-section h2 {
            margin-bottom: 20px;
            color: #333;
        }

        .stats-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
            gap: 20px;
        }

        .stat-card {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 20px;
            border-radius: 10px;
            text-align: center;
        }

        .stat-number {
            font-size: 32px;
            font-weight: bold;
            margin-bottom: 5px;
        }

        .stat-label {
            font-size: 14px;
            opacity: 0.9;
        }

        .recent-applications {
            background: white;
            border-radius: 10px;
            padding: 20px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
        }

        .recent-applications h3 {
            color: #333;
            margin-bottom: 15px;
        }

        .application-table {
            width: 100%;
            border-collapse: collapse;
        }

        .application-table th, .application-table td {
            padding: 12px 15px;
            text-align: left;
            border-bottom: 1px solid #eee;
        }

        .application-table th {
            background-color: #f8f9fa;
        }

        .status-pending {
            color: #ffc107;
        }

        .status-processing {
            color: #17a2b8;
        }

        .status-resolved {
            color: #28a745;
        }

        .status-rejected {
            color: #dc3545;
        }

        .btn {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 5px 10px;
            border-radius: 5px;
            text-decoration: none;
            font-size: 14px;
        }

        .loading {
            text-align: center;
        }

        .spinner {
            display: inline-block;
            width: 20px;
            height: 20px;
            border: 3px solid rgba(118, 75, 162, 0.3);
            border-radius: 50%;
            border-top-color: #764ba2;
            animation: spin 1s ease-in-out infinite;
            vertical-align: middle;
            margin-right: 8px;
        }

        @keyframes spin {
            to { transform: rotate(360deg); }
        }
    </style>
</head>
<body>
<div class="header">
    <h1>售后管理系统 - 客服端</h1>
    <div class="user-info">
        <span>欢迎，<span id="username">客服</span></span>
        <a href="/after_sale_system/login" class="logout-btn">退出登录</a>
    </div>
</div>

<div class="container">
    <div class="nav-menu">
        <h2>功能菜单</h2>
        <div class="nav-grid">
            <a href="/after_sale_system/page/customerService/pending-audit" class="nav-item">
                <h3>申请审核</h3>
                <p>审核售后申请</p>
            </a>
            <a href="/after_sale_system/page/customerService/workOrder" class="nav-item">
                <h3>工单处理</h3>
                <p>处理售后工单</p>
            </a>
        </div>
    </div>

    <div class="stats-section">
        <h2>客服工作台</h2>
        <div class="stats-grid">
            <div class="stat-card">
                <div class="stat-number" id="pendingCount">0</div>
                <div class="stat-label">待审核申请</div>
            </div>
            <div class="stat-card">
                <div class="stat-number" id="processingCount">0</div>
                <div class="stat-label">处理中申请</div>
            </div>
            <div class="stat-card">
                <div class="stat-number" id="todayNewCount">0</div>
                <div class="stat-label">今日新申请</div>
            </div>
            <div class="stat-card">
                <div class="stat-number" id="complaintCount">0</div>
                <div class="stat-label">待处理投诉</div>
            </div>
        </div>
    </div>

    <div class="recent-applications">
        <h3>最近申请</h3>
        <table class="application-table">
            <thead>
            <tr>
                <th>申请ID</th>
                <th>用户</th>
                <th>商品</th>
                <th>类型</th>
                <th>状态</th>
                <th>申请时间</th>
                <th>操作</th>
            </tr>
            </thead>
            <tbody id="recentApplicationsTable">
            <!-- 表格内容将通过JavaScript动态加载 -->
            <tr><td colspan="7" class="loading"><div class="spinner"></div> 加载中...</td></tr>
            </tbody>
        </table>
    </div>
</div>

<script>
    // 日期时间格式化工具函数
    function formatDateTime(dateString) {
        const date = new Date(dateString);
        return date.toLocaleString('zh-CN', {
            year: 'numeric',
            month: '2-digit',
            day: '2-digit',
            hour: '2-digit',
            minute: '2-digit'
        });
    }

    // 获取用户信息
    fetch('/after_sale_system/api/user/info')
        .then(response => response.json())
        .then(data => {
            if (data.success) {
                document.getElementById('username').textContent = data.data;
            }
        })
        .catch(error => {
            console.error('获取用户信息失败:', error);
        });

    // 页面加载时获取统计数据
    document.addEventListener('DOMContentLoaded', function() {
        // 获取统计数据
        fetch('/after_sale_system/api/statistics/applications?timeRange=daily')
            .then(response => response.json())
            .then(data => {
                if (data.success) {
                    document.getElementById('pendingCount').textContent = data.data.pendingCount || 0;
                    document.getElementById('processingCount').textContent = data.data.processingCount || 0;
                    document.getElementById('todayNewCount').textContent = data.data.todayNewCount || 0;
                }
            })
            .catch(error => {
                console.error('获取统计数据失败:', error);
            });

        // 获取待处理投诉数量
        fetch('/after_sale_system/api/complaint?status=PENDING')
            .then(response => response.json())
            .then(data => {
                if (data.success) {
                    document.getElementById('complaintCount').textContent = data.data.length || 0;
                }
            })
            .catch(error => {
                console.error('获取投诉数据失败:', error);
            });

        // 获取最近申请列表
        fetch('/after_sale_system/api/afterSale?status=PENDING_AUDIT')
            .then(response => response.json())
            .then(data => {
                if (data.success) {
                    const tableBody = document.getElementById('recentApplicationsTable');
                    tableBody.innerHTML = '';

                    // 只显示前5条
                    const recentApplications = data.data.slice(0, 5);

                    if (recentApplications.length === 0) {
                        tableBody.innerHTML = '<tr><td colspan="7" style="text-align:center">暂无申请</td></tr>';
                        return;
                    }

                    recentApplications.forEach(app => {
                        const row = document.createElement('tr');
                        row.innerHTML = `
                                <td>${app.id}</td>
                                <td>${app.userId}</td>
                                <td>${app.productName}</td>
                                <td>${app.afterSaleType}</td>
                                <td><span class="status-${app.status.toLowerCase()}">${app.status}</span></td>
                                <td>${formatDateTime(app.applyTime)}</td>
                                <td>
                                    <a href="/after_sale_system/page/customerService/applicationDetail?id=${app.id}" class="btn">查看</a>
                                </td>
                            `;
                        tableBody.appendChild(row);
                    });
                }
            })
            .catch(error => {
                console.error('获取申请列表失败:', error);
                document.getElementById('recentApplicationsTable').innerHTML =
                    '<tr><td colspan="7" style="text-align:center">加载失败</td></tr>';
            });
    });
</script>
</body>
</html>